#pragma once
#include <Windows.h>
#include <d3d9.h>
#include <d3dx9.h>
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")
#include "templatearray.h"
#include "templatevector.h"
#include "EngineData.h"

struct TextureInfo
{
	int					length;
	LPCWSTR				name;
	IDirect3DTexture9*	sprite;
	D3DXIMAGE_INFO		imageInfo;
	RECT				rect;
};

class DX9Engine
{
private:
	// pointer to D3D interface
	IDirect3D9*					m_pDX9Object;
	// pointer to device class
	IDirect3DDevice9*			m_pDX9Device;
	// direct3d sprite
	ID3DXSprite*				m_sprite;
	// font variable
	ID3DXFont*					m_pFont;
	// text color
	D3DCOLOR					m_fontColor;
	// Vector of textures
	TemplateVector<TextureInfo *> m_textures;
	//// To keep track of unreference pointers created by createSprite
	//TemplateVector<TextureInfo *>	trackTex;

	// Hidden class constructor
	DX9Engine() {}
	//// instance of DXEngine
	//static DX9Engine *instance;
	// instance flag
	static bool instanceFlag;

	TextureInfo *temp;

public:

	/** Get instance of DXEngine */
	static DX9Engine* getInstance();

	/** Get Texture Info */
	RECT getBounds(int id) {return m_textures.get(id)->rect;}
	int getWidth(int id) {return m_textures.get(id)->imageInfo.Width;}
	int getHeight(int id) {return m_textures.get(id)->imageInfo.Height;}

	/** Set up and Initialize D3D */
	void initialize(HWND hWnd);

	/** Render single frame */
	void startRender();
	void endRender();
	void startSprite();
	void endSprite();

	/** Draw Sprite */
	void renderSprite(int index, V2DF position, float scale, float angle,  
		RECT* sheet, bool centered, int r, int g, int b);

	/** Draw text to window 
	* @param text Text to be written to screen
	* @param rect Text bounds (location on screen)
	*/
	void writeText(char * text, RECT rect);
	/** Draw text centered at rect 
	* @param text Text to be written to screen
	* @param rect Text bounds (location on screen)
	*/
	void writeTextCenter(char* text, RECT rect);
	void setTextColor(D3DCOLOR color) {m_fontColor=color;}

	/** Close D3D and release memory */
	void release(void);

	/** Create Sprite */
	int createSprite(LPCWSTR fileName, int length);
};